using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

namespace TestProject
{
	/// <summary>
	/// Represents a sign-up entry in the database. 
	/// </summary>
	public class Signup
	{
		private int m_signup_ID;
		private string m_username;
		private string m_firstname;
		private string m_lastname;
		private string m_email;
		private string m_contactnumber;
		
		public Signup ()
		{
		}
		
		public Signup(int signup_ID)
		{
			m_signup_ID = signup_ID;
			getData();
		}
		
		public int signup_ID
		{
			set {
				m_signup_ID = value;
				getData();
			}
			get {
				return m_signup_ID;
			}
		}
		
		public string username
		{
			set {
				m_username = value;
			}
			get {
				return m_username;
			}
		}
		
		public string firstname
		{
			set {
				m_firstname = value;
			}
			get {
				return m_firstname;
			}
		}
		
		public string lastname
		{
			set {
				m_lastname = value;
			}
			get {
				return m_lastname;
			}
		}
		
		public string email
		{
			set {
				m_email = value;
			}
			get {
				return m_email;
			}
		}
		
		public string contactnumber
		{
			set {
				m_contactnumber = value;
			}
			get {
				return m_contactnumber;
			}
		}
		
		private void getData()
		{
	        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
	        try
			{
				conn.Open();
				string statement = "SELECT username, firstname, lastname, email, contactnumber "
					+ "FROM signup WHERE signup_ID = '" + this.m_signup_ID + "'";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);			
		        MySqlDataReader reader = cmd.ExecuteReader();
		        while(reader.Read()) {
					m_username = (string) reader["username"];
					m_firstname = (string) reader["firstname"];
					m_lastname = (string) reader["lastname"];
					m_email = (string) reader["email"];
					m_contactnumber = (string) reader["contactnumber"];
		        }
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
		}
		
		/// <summary>
		/// Finds out if the username of this signup entry already exists in the users table.
		/// </summary>
		/// <returns>
		/// A <see cref="System.Boolean"/> True if it already exists, false otherwise.
		/// </returns>
		public static bool usernameExists(string username)
		{
			MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
			bool toReturn = true;
	        try
			{
				conn.Open();
				string statement = "SELECT * "
					+ "FROM users WHERE username = '" + username + "'";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);			
		        MySqlDataReader reader = cmd.ExecuteReader();
				if (reader.HasRows)
				{			
					conn.Close();
					reader.Close();
					return toReturn;	
				}
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
			return false;
		}
		
		/// <summary>
		/// Adds a sign up entry in the database. 
		/// </summary>
		/// <param name="username">
		/// A <see cref="System.String"/> The username of the one signing up.
		/// </param>
		/// <param name="firstname">
		/// A <see cref="System.String"/> The first name of the one signing up.
		/// </param>
		/// <param name="lastname">
		/// A <see cref="System.String"/> The last name of the one signing up.
		/// </param>
		/// <param name="email">
		/// A <see cref="System.String"/> The email of the one signing up.
		/// </param>
		/// <param name="contactnumer">
		/// A <see cref="System.String"/> The contact numer of the one signing up.
		/// </param>
		public static bool addSignUp(string username, string firstname, string lastname, string email, string contactnumer)
		{
			MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
			if (Signup.usernameExists(username))
				return false;
	        try
			{
				conn.Open();
				string newStatement = "INSERT INTO signup (username, firstname, lastname, email, contactnumber) "
					+ "VALUES ('"+username+"','"+firstname+"','"+lastname+"','"+email+"','"+contactnumer+"')";
		        MySqlCommand newCmd = new MySqlCommand(newStatement, conn);
				newCmd.ExecuteNonQuery();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
			return true;
		}
		
		/// <summary>
		/// Deletes a sign up entry in the database. 
		/// </summary>
		/// <param name="signup_ID">
		/// A <see cref="System.Int32"/> The signup_ID of the signup entry to be deleted.
		/// </param>
		public static void deleteSignUp(int signup_ID)
		{
			MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
	        try
			{
				conn.Open();				
				string statement = "DELETE FROM signup where signup_ID='"+signup_ID+"'";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);
				cmd.ExecuteNonQuery();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();			
		}
		
		/// <summary>
		/// Gets all the sign ups currently in the database. 
		/// </summary>
		/// <returns>
		/// A <see cref="List<Signup>"/> All the sign ups in a List<Signup> object.
		/// </returns>
		public static List<Signup> getAllSignUp()
		{
			MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
			List<Signup> signUpList = new List<Signup>(0);
	        try
			{
				conn.Open();
				string statement = "SELECT * FROM signup";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);			
		        MySqlDataReader reader = cmd.ExecuteReader();
				while (reader.Read())
				{
					signUpList.Add(new Signup((int)reader["signup_ID"]));
				}
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
			return signUpList;
		}
	}
}

